package com.ganjiang.controller;

import java.io.IOException;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.ganjiang.entity.User;
import com.ganjiang.service.UserCenterService;
import com.ganjiang.service.UserService;

@Controller
public class UserController {

	@Resource
	private UserCenterService userCenterService;
	@Resource
	private UserService userService;

	@RequestMapping("/userlist")

	public ModelAndView list() {
		ModelAndView mav = new ModelAndView();
		mav.addObject("userList", userService.selectAllUser());

		mav.setViewName("userList");
		return mav;
	}

	@RequestMapping("/findAllUserWithCenter")

	public ModelAndView findAllUserWithCenter() {

		ModelAndView mav = new ModelAndView();
		mav.addObject("userWithCenterList", userService.findAllUserWithCenter());

		mav.setViewName("userwithCenter");
		return mav;
	}

	@RequestMapping("/getUser")
	public String findById(HttpServletRequest request, HttpServletResponse response) {

		int uid = Integer.valueOf(request.getParameter("uid"));

		User user = userService.findById(uid);

		request.setAttribute("use", user);

		return "userUpdate";

	}

	@RequestMapping("/findUserWithCenter")
	public String findUserWithCenter(HttpServletRequest request, HttpServletResponse response) {

		User user = userService.findUserWithCenter(1);

		request.setAttribute("userWithCenter", user);

		return "userwithCenter";

	}

	@RequestMapping("/toAdd")

	public String toAdd() {
		return "userAdd";
	}

	@RequestMapping("/updateUser")

	public void updateUser(HttpServletRequest request, HttpServletResponse response) throws IOException {

		int uid = Integer.valueOf(request.getParameter("uid"));
		String email = request.getParameter("email");
		String username = request.getParameter("username");
		String mobile = request.getParameter("mobile");
		int status = Integer.valueOf(request.getParameter("status"));
		int emailstatus = Integer.valueOf(request.getParameter("emailstatus"));
		int avatarstatus = Integer.valueOf(request.getParameter("avatarstatus"));
		int videophotostatus = Integer.valueOf(request.getParameter("videophotostatus"));

		User user = new User();

		user.setUid(uid);
		user.setEmail(email);
		user.setUsername(username);
		user.setMobile(mobile);
		user.setStatus(status);
		user.setEmailstatus(emailstatus);
		user.setAvatarstatus(avatarstatus);
		user.setVideophotostatus(videophotostatus);

		userService.update(user);
		response.sendRedirect("/userlist");
	}

	@RequestMapping("/userAdd")
	public void userAdd(HttpServletRequest request, HttpServletResponse response) throws IOException {

		int uid = Integer.valueOf(request.getParameter("uid"));
		String email = request.getParameter("email");
		String username = request.getParameter("username");
		String mobile = request.getParameter("mobile");
		int status = Integer.valueOf(request.getParameter("status"));
		int emailstatus = Integer.valueOf(request.getParameter("emailstatus"));
		int avatarstatus = Integer.valueOf(request.getParameter("avatarstatus"));
		int videophotostatus = Integer.valueOf(request.getParameter("videophotostatus"));

		User user = new User();

		user.setUid(uid);
		user.setEmail(email);
		user.setUsername(username);
		user.setMobile(mobile);
		user.setStatus(status);
		user.setEmailstatus(emailstatus);
		user.setAvatarstatus(avatarstatus);
		user.setVideophotostatus(videophotostatus);

		userService.addUser(user);

		response.sendRedirect("/userlist");

	}

	@RequestMapping("/delete")

	public void delete(HttpServletRequest request, HttpServletResponse response) throws IOException {
		int id = Integer.valueOf(request.getParameter("uid"));

		userService.delete(id);
		response.sendRedirect("userlist");

	}

}
